package io.renren.modules.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.Constant;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.admin.entity.CourseContentEntity;
import io.renren.modules.admin.entity.CourseDemoEntity;
import io.renren.modules.admin.entity.CourseEntity;
import io.renren.modules.admin.service.*;
import io.renren.modules.web.annotation.LoginUser;
import io.renren.modules.web.entity.UserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("user/course")
@Api("用户课程")
public class UserCourseController {

    @Autowired
    private MyCourseService myCourseService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private CourseVideoService  courseVideoService;

    @Autowired
    private CourseContentService courseContentService;

    @Autowired
    private CourseDemoService courseDemoService;

    /**
     * 查询用户 课程列表
     */
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
    })
    @ApiOperation("获取用户课程")
    public R userCourseList(  @ApiIgnore @RequestParam Map<String, Object> params ,@ApiIgnore @LoginUser UserEntity userEntity){

        Long userId = userEntity.getUserId();
        if (userId ==null || userId.equals(0)){
            return R.error("用户未登录");
        }
        params.put("userId",userId);
        PageUtils page = courseService.getCourseList(params);

        return new R().put("page",page);
    }

    @ApiOperation("查询上下册详细内容") //栏目
    @GetMapping("/list/{courseId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query" , dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query" , dataType="int") ,
    })
    public R getCourseProgram( @PathVariable  Long courseId, @ApiIgnore @RequestParam Map<String, Object> params, @ApiIgnore@LoginUser UserEntity userEntity){
        //老师查 课程的栏目  和 上下册的图片
        List<CourseEntity> courseEntities = courseService.teacherCourseProgram(courseId);
        R r=new R().put("program",courseEntities);
        return r;
    }


    //查询 课程内容列表 标题和 序号
    @GetMapping("/content/list/{courseId}")
    @ApiOperation("查询 课程内容列表 标题和 序号")
    public R getCourseContent(@PathVariable("courseId") Long courseId  ){

        QueryWrapper<CourseContentEntity> wrapper = new QueryWrapper<>();
        wrapper.select("course_id","content_title","img_url","content_no","course_content_id");
        wrapper.eq("course_id",courseId );
        wrapper.orderByAsc("content_no");

        List<CourseContentEntity> list = courseContentService.list(wrapper);

        return new R().put("contentList",list);
    }

    @GetMapping("/content/info/{courseContentId}")
    @ApiOperation("查询 课程内容 详情")
    public R getContentDetail(@PathVariable("courseContentId") Long courseContentId){
        CourseContentEntity courseContent = courseContentService.getById(courseContentId);
        //查询课程内容
        QueryWrapper<CourseDemoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("content_id",courseContentId);
        CourseDemoEntity demoEntity = courseDemoService.getOne(wrapper);
        courseContent.setCourseDemoEntity(demoEntity);
        return R.ok().put("courseContent", courseContent);
    }

    /**
     * 查询课程视频列表
     */
    @ApiOperation("根据栏目id 查询视频")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query" , dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query" , dataType="int") ,
    })
    @GetMapping("video/{programId}")
    public R getVideos(@ApiIgnore @RequestParam Map<String, Object> params,@PathVariable Long programId){
        params.put("courseId",programId);
        PageUtils page = courseVideoService.queryPage(params);

        return new R().put("page",page);
    }

    /**
     * 查询 学生课程
     */
    @GetMapping("video/")
    @ApiOperation("查询 学生课程")
    public R getStuCourse(){
        QueryWrapper<CourseEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("type","0");
        wrapper.eq("side","1");
        List<CourseEntity> list = courseService.list(wrapper);

        return new R().put("list",list);
    }
}
